package com.vote.admin.service;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vote.admin.vo.param.LoginParam;
import com.vote.common.constant.RedisCacheTime;
import com.vote.common.constant.RedisConstants;
import com.vote.common.entity.VoteMenu;
import com.vote.common.entity.VoteUser;
import com.vote.common.enums.AdminSessionEnum;
import com.vote.common.enums.UserSessionEnum;
import com.vote.common.enums.UserTypeEnum;
import com.vote.common.returns.RespCode;
import com.vote.common.returns.RespEntity;
import com.vote.common.service.VoteMenuService;
import com.vote.common.service.VoteUserService;
import com.vote.common.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class AdminLoginService {

    @Autowired
    private VoteUserService voteUserService;

    /**
     * 生成验证码
     * @param response
     * @param session
     */
    public RespEntity createCode(HttpServletResponse response, HttpSession session){
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            captcha.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            log.error("生成验证码失败", e.getMessage());
        }
        session.setAttribute(AdminSessionEnum.ADMIN_OLD_CODE.getCode(), captcha.getCode());
        return new RespEntity(RespCode.SUCCESS);
    }

    /**
     * 登陆
     * @param loginParam
     * @param session
     * @return
     */
    public RespEntity login(LoginParam loginParam, HttpSession session) {

        //校验验证码
        String oldCode = (String) session.getAttribute(AdminSessionEnum.ADMIN_OLD_CODE.getCode());
        if (! StringUtils.equals(oldCode, loginParam.getCode())) {
            return new RespEntity(RespCode.CODE_ERROR);
        }

        //校验账号密码
        QueryWrapper<VoteUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(VoteUser::getUsername, loginParam.getUsername())
                .eq(VoteUser::getPassword, loginParam.getPassword());
        VoteUser voteUser = voteUserService.getOne(queryWrapper);
        if (Objects.isNull(voteUser)) {
            return new RespEntity(RespCode.LOGIN_ERROR);
        }

        //校验权限
        if (UserTypeEnum.USER.getCode().equals(voteUser.getType())) {
            return new RespEntity(RespCode.NOT_ADMIN);
        }

        //保存登陆会话状态
        session.setAttribute(AdminSessionEnum.ADMIN.getCode(), voteUser);

        return new RespEntity(RespCode.SUCCESS);
    }

}
